(function() {

	var skill = {
		save: function() {
			delete this._MA;
			db.update(config.dbname.user, this._id, this);
		},
		destroy: function() {
			db.remove(config.dbname.user, this._id);
		},
		//改变用户基本信息
		setInfo: function(userInfo) {
			//名字不区分大小写
			// this.info.name = userInfo.name.trim().toLowerCase();//不提供用户名的修改
			var _cache_instance;
			userInfo.real_name || (userInfo.real_name = "");
			userInfo.nick_name || (userInfo.nick_name = "");
			userInfo.mobile_phone || (userInfo.mobile_phone = "");
			userInfo.email || (userInfo.email = "");
			userInfo.id_card || (userInfo.id_card = "");
			userInfo.header_url || (userInfo.header_url = "");
			userInfo.alipay || (userInfo.alipay = "");
			userInfo.weixin_unionid || (userInfo.weixin_unionid = "");
			userInfo.weixin_openid || (userInfo.weixin_openid = "");
			//真实姓名
			this.info.real_name = userInfo.real_name.trim() || "";
			//昵称
			this.info.nick_name = userInfo.nick_name.trim() || "";
			//绑定的手机
			if (this.info.mobile_phone !== userInfo.mobile_phone) {
				if (userInfo.mobile_phone = userInfo.mobile_phone.trim()) {
					if (!helper.phone(userInfo.mobile_phone)) {
						throwCusError("手机号码格式不正确");
					}
					if ((_cache_instance = db.find_one(config.dbname.user, {
							info: {
								mobile_phone: userInfo.mobile_phone
							}
						})) && _cache_instance._id !== this._id) {
						throwCusError("手机号码已被使用")
					}
					this.info.mobile_phone = userInfo.mobile_phone
				} else {
					this.info.mobile_phone = "";
				}
			}
			//绑定的邮箱
			if (this.info.email !== userInfo.email) {
				if (userInfo.email = userInfo.email.trim()) {
					if (!helper.isEmail(userInfo.email)) {
						throwCusError("邮箱格式不正确")
					}
					if ((_cache_instance = db.find_one(config.dbname.user, {
							info: {
								email: userInfo.email
							}
						})) && _cache_instance._id !== this._id) {
						throwCusError("邮箱已被使用")
					}
					this.info.email = userInfo.email;
				} else {
					this.info.email = "";
				}
			}
			//身份证
			if (this.info.id_card !== userInfo.id_card) {
				if (userInfo.id_card = userInfo.id_card.trim()) {
					var id_card_info = helper.id_card(userInfo.id_card);
					if (id_card_info !== true) {
						throwCusError(id_card_info);
					}
					if ((_cache_instance = db.find_one(config.dbname.user, {
							info: {
								id_card: userInfo.id_card
							}
						})) && _cache_instance._id !== this._id) {
						throwCusError("身份证号码已被使用")
					}
					this.info.id_card = userInfo.id_card;
				} else {
					this.info.id_card = "";
				}
			}
			//用户头像
			this.info.header_url = userInfo.header_url;
			//支付宝信息
			this.info.alipay = userInfo.alipay.trim();
			//微信信息
			this.info.weixin_unionid = userInfo.weixin_unionid.trim();
			this.info.weixin_openid = userInfo.weixin_openid.trim();
			this.save();
		},
		//改动用户密码
		setPassword: function(old_pwd, new_pwd) {
			console.log(old_pwd, new_pwd);
			if (this.info.password !== md5.md5_2(old_pwd)) {
				throwCusError("密码不正确，无权进行修改");
			}
			this._setPassword(new_pwd);
		},
		//非安全，强制改密码
		_setPassword: function(new_pwd) {
			this.info.password = md5.md5_2(new_pwd);
			this.save();
		},
		//绑定用户微信账号
		setWeiXinUnionIDByOpenId: function(wx_v2, openid) {
			var self = this;
			return process.domain.run(function() {
				console.log("openid:", openid);
				var wx_user_info = wx_v2.getUserInfo(openid);
				console.log("wx_user_info:", wx_user_info.unionid, wx_user_info);
				var unionid = wx_user_info.unionid;
				if (!unionid) {
					throwCusError("商家微信公众号还未绑定到点纳微信开放平台帐号")
					return
				}
				if (self.info.weixin_unionid == wx_user_info.unionid) {
					//无需绑定
					return;
				}

				//接触其它人的绑定
				var other_bind_wx_user_obj = db.find_one(config.dbname.user, {
					info: {
						weixin_unionid: wx_user_info.unionid
					}
				});
				if (other_bind_wx_user_obj) {
					var other_bind_wx_user = UserCon.getInstance(other_bind_wx_user_obj);
					other_bind_wx_user.info.weixin_unionid = "";
					other_bind_wx_user.save();
				}
				//绑定到自己上
				self.info.weixin_unionid = wx_user_info.unionid;

				if (!self.info.header_url && wx_user_info.headimgurl) {
					var fiber = Fiber.current;
					//如果用户没有设定头像的话，默认用微信的头像	
					var bs = new BufferStreams(function(err, buf, cb) {
						if (err) {
							throw err
						}
						cb(buf);
					});
					//保存到BUFF中
					request.get(wx_user_info.headimgurl).pipe(bs);
					//上传BUFF
					bs.on("finish", function() {
						console.log(bs._buf.length);
						qiniu.uploadBuff(bs._buf, function(err, res) {
							self.info.header_url = config.img_server_url + res.key;
							fiber.run();
						});
					});
					Fiber.yield();
				}

				self.save();
			});
		},
		//改变用户关联人的信息
		setSomeBody: function(info) {
			//推荐者
			var recommender_id = info.recommender_id;
			if (recommender_id && UserCon.get(recommender_id)) {
				this.info.recommender_id = recommender_id
			}
			//导购者
			var guide_id = info.guide_id;
			if (guide_id && BusGuideCon.get(guide_id)) {
				this.info.guide_id = guide_id
			}
		},
		getRecommenderLink: function(layer) {
			layer = ~~layer || 3; // 默认为3级，1~3
			var info = this.info;
			var res = [];
			while ((layer -= 1) >= 0) {
				if (info.recommender_id) {
					var recommender = UserCon.get(info.recommender_id);
					info = recommender.info;
					res.push(recommender);
				} else {
					return res
				}
			}
			return res;
		},
		/*
		 *	购买商品：
		 */
		//下订单
		//收货地址参数可为空，为空时使用默认收货地址
		//return ach
		//PS:订单完成后商家可以看到新增的订单，并做相应的修改，直到用户支付订单
		makeAchFromGoods: function(info) {
			var goods_id = info.goods_id;
			var user_address_id = info.user_address_id;
			var user = this;
			console.log(user.info.name, "想要购买一个", goods_id, "”商品");
			var goods = GoodsCon.get(goods_id);
			user_address_id || (info.user_address_id = this.default_ua_id);
			if (goods) {
				info.user_id = this._id;
				var ach_id = goods._toAch(info);
				this._addAch(ach_id);
				return ach_id;
			} else {
				throwCusError("找不到指定商品");
			}
		},
		//下订单
		//收货地址参数可为空，为空时使用默认收货地址
		//统一处理生产的订单，添加BUFF
		_afterMakeAch: function(ach_id) {
			//会员卡的优惠，自动绑定
			var ach = AchCon.get(ach_id);
			var card = this.getUseAbleCardListForAch(ach_id, "fouce_get_ach")[0];
			if (card) {
				ach._bindCard(card._id);
				console.log("ach.card_id:", ach.card_id)
			}
		},
		//return ach
		makeAchFromCartGoods: function(info, is_for_cash_com, no_remove) {
			var cart_id = info.cart_id;
			//收货地址
			var user_address_id = info.user_address_id;
			user_address_id || (info.user_address_id = this.default_ua_id);
			var cart_goods = this._getCartGoods(cart_id);
			info.user_id = this._id;
			var ach_id = cart_goods._toAch(info, is_for_cash_com);
			this._afterMakeAch(ach_id);
			if (!is_for_cash_com) { //如果不是计算订单价格，来真的，移除购物车商品区生成订单
				this._addAch(ach_id);
			}
			if (!no_remove) {
				//移除这个购物车对象，可能是临时的
				this.removeCart(cart_id);
			};
			PayAchCon.build(info.user_id, ach_id);
			return ach_id;
		},
		makeAchFromCartGoodsList: function(info, is_for_cash_com) {
			var cart_id_list = info.cart_id_list;
			var user = this;
			// var user_address_id = info.user_address_id;
			if (cart_id_list.length > 1) {
				//合并购物车商品，临时生成一个新的购物车对象
				var cart_id = UserCartGoodsCon.concat(this._id, cart_id_list);
				if (!is_for_cash_com) { //如果不是计算订单价格，来真的，移除购物车商品区生成订单
					//移除
					cart_id_list.forEach(function(cart_goods_id) {
						user.removeCart(cart_goods_id);
					});
				}
			} else {
				cart_id = cart_id_list[0];
				if (is_for_cash_com) {
					//这不是临时对象，不能移除
					var no_remove = true;
				}
			}
			info.cart_id = cart_id;
			return this.makeAchFromCartGoods(info, is_for_cash_com, no_remove);
		},
		//改变推荐积分
		_recInt: function(num) {
			num = isNaN(num) ? 0 : +num;
			return this.rec_int += num;
		},
		//获取该用户在某个商家的推荐额度
		getBusAccInt: function(bus_id) {
			var bind_id = BusUserBindCon.build({
				bus_id: bus_id,
				user_id: this._id
			});
			return BusUserBindCon.get(bind_id).acc;
		},
		//改变购买积分
		_buyInt: function(num) {
			num = isNaN(num) ? 0 : +num;
			return this.buy_int += num;
		},
		//生成针对商家的推荐对象
		buildRecommendBusInstance: function(bus_id) {
			var bus = BusCon.get(bus_id);
			if (!bus) {
				throwCusError("找不到指定商家");
			}
			var bus_user_bind = BusUserBindCon.build({
				bus_id: bus_id,
				user_id: this._id
			});
			return bus_user_bind._id;
		},
		//生成针对商品推荐链接
		buildRecommendGoodsURL: function(bus, goods_name) {
			var self = this;
			var url_hash = self._id.substr(2) + Math.random().toString(16).substr(2) + (+new Date).toString(16).substr(7);
			db.insert(config.dbname.RecommendGoodsURL, {
				_id: config.dbIdPrefix.RecommendGoodsURL + url_hash,
				user_id: self._id,
				bus_id: bus._id,
				goods_name: goods_name,
				url_hash: url_hash
			});
			console.log("用户", self.info.name, "推荐了商品“", goods_name, "”，推荐链接为：baseurl.com/" + url_hash);
			return url_hash;
		},
		//获取购物车的数据
		getCart: function(num, page) {
			var self = this;
			var result = (this.cart || []).slice();
			result = data_cut.page(result, num, page);
			var carts = [];
			result.forEach(function(cart_id) {
				var cart = UserCartGoodsCon.get(cart_id);
				if (!cart) {
					self.removeCart(cart_id)
				} else {
					carts.push(cart);
				}
			});
			return carts;
		},
		getCartDetail: function(num, page) {
			var cart = this.getCart(num, page);
			var _update; //是否更新数据
			cart.forEach(function(cart_goods) {
				var old_len = cart_goods.goods_info.length;
				cart_goods.goods_info = cart_goods.goods_info.filter(function(goods_info) {
					var goods = GoodsCon.get(goods_info.goods_id);
					console.log("getCartDetail goods:", goods && goods._id);
					if (goods) {
						goods_info.goods = goods;
						//整理出最大购买数
						goods_info.max_num = goods.limit ? (+goods.limit) : (+goods.num);
					}
					return goods;
				});
				if (old_len !== cart_goods.goods_info.length) {
					_update = true;
				}
			});
			if (_update) {
				this.save();
			}
			return cart;
		},
		//只获取某个商家的购物车
		getCartDetailByBus: function(bus_id, num, page) {
			var cart = this.getCartDetail(num, page);
			cart = cart.filter(function(cart_goods) {
				return (cart_goods.bus_id === bus_id);
			});
			return cart;
		},
		//清空购物车
		clearCart: function() {
			this.cart = this.cart.filter(function(cart_info) {
				UserCartGoodsCon.get(cart_info).destroy();
				return false;
			});
			this.save();
		},
		//添加商品数组到购物车中
		addGoodsToCart: function(goods_info, bus_id) {
			var self = this;
			var cart = (this.cart || (this.cart = [])).slice();
			var new_cart;
			var goods_index;

			if (cart.every(function(cart_id, index) { //查找类似结构可合并商品的购物车对象，如果每一个都返回true，就说明有没合适的购物车对象可盈用来合并，所以就新增一个
					var current_cart = UserCartGoodsCon.get(cart_id);
					if (!current_cart.goods_info.length) { //空的购物车，移除掉
						self.removeCart(cart_id);
					}
					//判断购物车商品的结构是否相同，可否合并
					if (current_cart.goods_info.length == goods_info.length) {
						var goods_id_map = {};
						//将Array<goods_info>转化为HASH来进行比较，并校验数据
						current_cart.goods_info.forEach(function(goods_id_and_num) {
							var num = ~~goods_id_and_num.num;
							goods_id_map[goods_id_and_num.goods_id] = num;
						});

						if (goods_info.every(function(goods_id_and_num) { //如果都是true，说明结构是完全一样的，那么就可以进行合并
								var result = goods_id_map.hasOwnProperty(goods_id_and_num.goods_id);
								if (result) {
									var num = (~~goods_id_and_num.num) || 1;
									if (num <= 0) {
										throwCusError("购买的商品数量有误");
									}
									goods_id_map[goods_id_and_num.goods_id] += num;
								}
								return result;
							})) {
							new_cart = current_cart;
							//将HASH转化为Array<goods_info>;
							var new_goods_info = [];
							Object.keys(goods_id_map).forEach(function(goods_id) {
								new_goods_info.push({
									goods_id: goods_id,
									num: goods_id_map[goods_id]
								});
							});
							new_cart.setGoodsInfo(new_goods_info);
							//终止遍历
							return false;
						} else {
							return true;
						}
					} else {
						return true;
					}
				})) {
				//如果购物车中找不到相似的记录，直接创建一个新的购物车商品对象
				if (!bus_id) { //获取默认商家ID
					bus_id = GoodsCon.get(goods_info[0].goods_id).bus_id;
				}

				new_cart = new UserCartGoodsCon({
					goods_info: goods_info,
					bus_id: bus_id
				});
				cart.unshift(new_cart._id);
				this.cart = cart;
				console.log(this.cart);
				this.save();
			} else {
				// "此商品已经在购物车中"
			}
			return new_cart._id;
		},
		//代码片段，根据购物车商品ID获取对象
		_getCartGoods: function(cart_id) {
			var cart = (this.cart || []);
			if (cart.indexOf(cart_id) === -1) {
				throwCusError("找不到指定的购物车商品在所在购物车中")
			}
			var cart_goods = UserCartGoodsCon.get(cart_id);
			if (!cart_goods) {
				throwCusError("找不到指定购物车商品")
			};
			return cart_goods;
		},
		// //获取购物车中的商品的价格
		// getCashFromCart:function (cart_id) {
		// 	var cart_goods = UserCartGoodsCon.get(cart_id);
		// 	cart_goods.goods_info.forEach(function (goods_id_and_num) {
		// 		var goods = goods_id_and_num.goods_id;
		// 		goods.
		// 	});
		// },
		//将商品从购物车中移除
		removeGoodsFromCart: function(cart_id, goods_id) {
			var cart_goods = UserCartGoodsCon.get(cart_id);
			if (!cart_goods) {
				throwCusError("购物车对象不存在，刷新页面看看")
			}
			cart_goods._removeGoods(goods_id);
			//商品数量空了
			if (!cart_goods.goods_info.length) {
				this.removeCart(cart_id);
			}
		},
		updateGoodsNumInCart: function(cart_id, goods_id, num) {
			var cart_goods = UserCartGoodsCon.get(cart_id);
			cart_goods._updateGoodsNum(goods_id, num);
		},
		//修改购物车的商品信息
		updateGoodsInfoInCart: function(cart_id, goods_info) {
			var cart_goods = this._getCartGoods(cart_id);
			cart_goods.setGoodsInfo(goods_info);
		},
		removeCart: function(cart_id) {
			var cart = (this.cart || (this.cart = []));
			var index = cart.indexOf(cart_id);
			if (index === -1) {
				throwCusError("找不到指定的购物车商品")
			}
			//销毁购物车商品对象
			var cart_goods = UserCartGoodsCon.get(cart_id);
			if (cart_goods) {
				cart_goods.destroy();
			}
			cart.splice(index, 1);
			this.save();
		},
		//获取收藏夹的数据
		getCollect: function(num, page) {
			var collect = (this.collect || []).slice();
			var collect_full = collect.filter(function(goods_id) {
				if (GoodsCon.get(goods_id)) {
					return true;
				}
			});
			if (collect_full.length !== collect.length) {
				this.collect = collect_full;
				this.save();
			}
			console.log("collect_full:", collect_full);
			var result = data_cut.page(collect_full, num, page);
			return result;
		},
		getCollectDetail: function(num, page) {
			var collect = this.getCollect(num, page);
			var collect_detail = collect.map(function(goods_id) {
				var goods = GoodsCon.get(goods_id);
				goods.url = goods.getUrl();
				return goods
			});
			return collect_detail;
		},
		getCollectDetail_byBusId: function(bus_id, num, page) {
			var collect = this.getCollect(num, page);
			var collect_detail = [];
			collect.forEach(function(goods_id) {
				var goods = GoodsCon.get(goods_id);
				if (bus_id == goods.bus_id) {
					goods.url = goods.getUrl();
					collect_detail.push(goods)
				}
			});
			return collect_detail;
		},
		addGoodsToCollect: function(goods_id) {
			var collect = (this.collect || (this.collect = []));
			if (!GoodsCon.get(goods_id)) {
				throwCusError("找不到指定商品");
			}
			if (collect.every(function(_goods_id) {
					return _goods_id !== goods_id;
				})) {
				collect.unshift(goods_id);
				this.collect = collect;
				this.save();
				return collect;
			} else {
				throwCusError("已收藏过此商品")
			}
		},
		removeGoodsFromCollect: function(goods_id) {
			var collect = (this.collect || (this.collect = []));
			var result = collect.filter(function(_goods_id) {
				return _goods_id !== goods_id;
			});
			if (result.length !== collect.length) {
				this.collect = result;
				this.save();
				return result;
			}
			throwCusError("指定商品不在指定收藏夹中")
		},
		//获取商家收藏夹的数据
		getCollectBus: function(num, page) {
			var result = (this.collectBus || []).slice();
			result = data_cut.page(result, num, page);
			return result;
		},
		getCollectBusDetail: function(num, page, goods_num) {
			var collectBus = this.getCollectBus(num, page);
			var collectBus_detail = collectBus.map(function(bus_id) {
				var bus = BusCon.get(bus_id);
				if (bus) {
					bus.latest_goods_list = bus.getGoodsList(goods_num || 5);
					bus.url = bus.getOneVisitableUrl();
					bus.latest_goods_list.forEach(function(goods) {
						goods.url = goods.getUrl(bus.url);
					});
				}
				return bus;
			});
			return $Array.clearNull(collectBus_detail);
		},
		addBusToCollectBus: function(bus_id) {
			var collectBus = (this.collectBus || (this.collectBus = []));
			if (!BusCon.get(bus_id)) {
				throwCusError("找不到指定商家");
			}
			console.log(collectBus);
			if (collectBus.every(function(_bus_id) {
					return _bus_id !== bus_id;
				})) {
				collectBus.unshift(bus_id);
				this.collectBus = collectBus;
				this.save();
				return collectBus;
			} else {
				throwCusError("已收藏过此商家")
			}
		},
		removeBusFromCollectBus: function(bus_id) {
			var collectBus = (this.collectBus || (this.collectBus = []));
			var result = collectBus.filter(function(_bus_id) {
				return _bus_id !== bus_id;
			});
			if (result.length !== collectBus.length) {
				this.collectBus = result;
				this.save();
				return result;
			}
			throwCusError("指定商品不在指定收藏夹中")
		},
		//获取单个收货地址
		getOneAddress: function(user_address_id) {
			var uas = this.uas || [];
			if (uas.indexOf(user_address_id) === -1) {
				throwCusError("收获地址不在指定收货地址列表中")
			}
			var user_address = UserAddressCon.get(user_address_id);
			if (!user_address) {
				throwCusError("找不到指定的收获地址")
			}
			return user_address;
		},
		getAddress: function(num, page) {
			var result = (this.uas || (this.uas = [])).slice();
			console.log(result, num);
			result = data_cut.page(result, num, page);
			return result;
		},
		addAddress: function(uaInfo) {
			var uas = (this.uas || (this.uas = [])).slice();
			//地址的存储有上限限制
			if (uas.length >= config.userAddressNumber) {
				throwCusError("收货地址存储条数到达上线");
			}
			var ua = new UserAddressCon(uaInfo);
			var ua_id = ua._id;
			uas.unshift(ua_id);
			this.uas = uas;
			this.save();
			return ua_id;
		},
		removeAddress: function(ua_id) {
			var uas = (this.uas || (this.uas = []));
			var result = $Array.remove(uas, ua_id);
			if (result.length !== uas.length) {
				if (this.default_ua_id === ua_id) {
					//默认收货地址被删除，则滞空
					this.default_ua_id = undefined;
				}
				this.uas = result;
				this.save();
				return result;
			}
			throwCusError("指定收获地址不属于指定用户");
		},
		setDefaultAddress: function(address_id) {
			if (this.uas && this.uas.indexOf(address_id) !== -1) {
				this.default_ua_id = address_id;
				this.save();
				return address_id;
			} else {
				throwCusError("找不到指定的收获地址");
			}
		},
		getDefaultAddress: function() {
			var address_id = this.default_ua_id;
			var user_address = address_id && UserAddressCon.get(address_id);
			// if (!user_address) {
			// 	return "找不到默认收获地址"
			// }
			return user_address;
		},
		//添加评论==评论、回复评价
		addComment: function(reply_id, content, isAny) {
			var comment = new CommentCon({
				type: "user",
				commenter_id: this._id,
				content: content,
				isAny: isAny,
				reply_id: reply_id
			});
			return comment._id;
		},
		//添加评论，非安全
		_addComment: function(comment_id) {
			var comments = this.comments || (this.comments = []);
			comments.unshift(comment_id);
			this.save();
		},
		//获取详细子评论评论列表（不包括评价，即顶级评论）
		getComments: function(num, page) {
			var result = [];
			do {
				var comments = this.comments || (this.comments = []);
				comments = data_cut.page(comments, num, page);
				comments.forEach(function(comment_id, index) {
					var comment = CommentCon.get(comment_id);
					comment = comment.getDetail();
					//如果不属于评价，加入到队列中
					if (!comment.reply_id) {
						var replyComment = CommentCon.get(comment.reply_id);
						//如果为空，则代表原评论已经删除
						comment.replyComment = replyComment;
						result.push(comment)
					}
				});
				page = (~~page) + 1;
			} while (num && result <= num);
			return comments;
		},
		//收到评论的回复
		_addReply: function(comment_id) {
			console.log("收到评论回复：", comment_id);
			var replys = this.replys || (this.replys = []);
			replys.unshift(comment_id);
			this.save();
		},
		//移除收到的回复
		_removeReply: function(comment_id) {
			var replys = this.replys || (this.replys = []);
			var index = replys.indexOf(comment_id);
			if (index) {
				replys.splice(index, 1);
				this.save();
			} else {
				throwCusError("回复不属于指定的对象");
			}
		},
		getReplys: function(num, page) {
			var replys = this.replys || (this.replys = []);
			replys = data_cut.page(replys, num, page);
			replys.forEach(function(comment_id, index) {
				var comments = CommentCon.get(comment_id);
				replys[index] = comments.getDetail();
			});
			return replys;
		},
		//代码片段，根据ID获取指定BUDD
		_getBuff: function(buff_id) {
			var buffs = this.buffs || [];
			if (buffs.indexOf(buff_id) === -1) {
				throwCusError("BUFF不属于指定用户")
			}
			var buff = BuffCon.get(buff_id);
			if (!buff) {
				throwCusError("找不到指定BUFF")
			}
			return buff;
		},
		//非安全，添加BUFF
		_addBuff: function(buff_id) {
			var buffs = this.buffs || (this.buffs = []);
			buffs.push(buff_id);
			this.save();
		},
		//对订单使用BUFF
		useBuffForAch: function(buff_id, ach_id) {
			var ach = this._getAch(ach_id);
			// buff.emit({
			// 	user_id:this._id,
			// 	ach_id:ach_id
			// });
			ach._addBuff(buff_id);
		},
		/*
		 * 导购链接
		 */
		state_guide_weishenqingduihuan_to_shenqingduihuanzhong: function(bus_id, apply_acc) {
			var guide = BusGuideCon.get(BusGuideCon.build({
				bus_id: bus_id,
				user_id: this._id
			}));
			console.log("guide.state", guide.state);
			if (!guide.state) {
				guide.state = BusGuideCon.STATE.未申请兑换;
			}
			if (guide.state == BusGuideCon.STATE.未申请兑换) {
				guide._nextFlowStatus();
				//目前默认全部兑换
				guide.apply_acc = apply_acc || guide.acc;
				guide.save();
			} else {
				throwCusError("兑现所在状态无法进行相应转换");
			}
			guide.bus = BusCon.get(guide.bus_id);
			if (!guide.acc) {
				throwCusError("无可兑换的红利")
			}
			guide._money = guide.bus.recommendAccToMoney(guide.acc);
			return guide;
		},
		state_guide_shangjiayijingfukuan_to_yonghuquerenfukuan: function(bus_id) {
			var guide = BusGuideCon.get(BusGuideCon.build({
				bus_id: bus_id,
				user_id: this._id
			}));
			if (!guide.state) {
				guide.state = BusGuideCon.STATE.未申请兑换;
			}
			if (guide.state == BusGuideCon.STATE.商家已经付款) {
				guide._nextFlowStatus();
				guide.total_acc = guide.total_acc || 0;
				guide.total_acc += guide.apply_acc;
				guide.acc -= guide.apply_acc;
				guide.state = BusGuideCon.STATE.未申请兑换;
				guide.save();
			} else {
				throwCusError("兑现所在状态无法进行相应转换");
			}
			guide.bus = BusCon.get(guide.bus_id);
			guide._money = guide.bus.recommendAccToMoney(guide.acc);
			return guide;
		},
		getGuideBusList: function(num, page) {
			var guide_list = BusGuideCon.findByUserId(this._id);
			guide_list = data_cut.page(guide_list, num, page);
			var result = [];
			guide_list.forEach(function(guide) {
				console.log(guide);
				guide.bus = BusCon.get(guide.bus_id);
				if (!guide.bus) {
					BusGuideCon.get(guide._id).destroy();
				} else {
					guide._money = guide.bus.guideAccToMoney(guide.acc);
					result.push(guide);
				}
			})
			return result;
		},
	};
	module.exports = skill.multiInherits(
		require("./skill.coupon"),
		require("./skill.ticket"),
		require("./skill.card"),
		require("./skill.ach"),
		require("./skill.bus_user_bind"),
		require("./skill.bus_user"),
		require("./skill.eval"),
		require("./skill.register_user"),
		require("./skill.extend.bina"),
		require("./skill.extend.MA"),
		require("./skill.lottery")

	);

	var db = require("../db/index");
	var UserCon = require("./_con");
	var CardCon = require("../card/_con");
	var UserCartGoodsCon = require("../user.cart_goods/_con");
	var UserAddressCon = require("../user_address/_con");
	var BusCon = require("../bus/_con");
	var BusGuideCon = require("../bus.guide/_con");
	var GoodsCon = require("../goods/_con");
	var AchCon = require("../ach/_con");
	var PayAchCon = require("../pay_ach/_con");
	var CommentCon = require("../comment/_con");
	var EvaluationCon = require("../eval/_con");
	var BuffCon = require("../buff/_con");
	var BusUserBindCon = require("../bus_user_bind/_con");
	var config = require("../_include/config");
	var data_cut = require("../_include/data_cut");
	var $Array = require("../_include/$.Array");
	var $Object = require("../_include/$.Object");
	var md5 = require("../_include/md5");
	var helper = require("../_include/helper");

	var WX_V2 = require("../../my_modules/wx/wx.v2");

	var Fiber = require("fibers")
	var request = require("request");
	var BufferStreams = require('bufferstreams');
	var qiniu = require("../../server/RESTAPI/qiniu");

}());